/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project_temple.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import project_temple.utilityserver.GoogleWeatherReader;
import project_temple.utilityserver.WeatherModel;

/**
 *
 * @author STARTHERE
 */
public class serverHandler extends Thread {

    Socket incoming;
    int counter;

    public serverHandler(Socket i, int c) {
        incoming = i;
        counter = c;
    }

    @Override
    public void run() {
        DataOutputStream dos = null;
        DataInputStream din = null;
        String com = null;
//        GoogleWeatherReader google=new GoogleWeatherReader("danang");
//        google.process();
        WeatherModel model = null;
        try {
            din = new DataInputStream(incoming.getInputStream());
            com = din.readUTF();
            GoogleWeatherReader google = new GoogleWeatherReader(com);
            google.process();
            model = google.getWeatherModel();
        } catch (IOException ex) {
            Logger.getLogger(serverHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            dos = new DataOutputStream(incoming.getOutputStream());
            if(model.toString()!=null)
                dos.writeUTF(model.toString());
            else     dos.writeUTF("xin loi, yeu cau cua ban chung toi khong the dap ung, vui long nhap lai ");
        } catch (IOException ex) {
            Logger.getLogger(serverHandler.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                incoming.close();
                dos.close();
            } catch (IOException ex) {
                Logger.getLogger(serverHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
